/**
 * 初始化车次核销详情对话框
 */
var TTrainInfoDlg = {
    id: "TTrainCheckTable",	//表格id
    table: null,
    tTrainInfoData : {}
};


/**
 * 初始化表格的列
 */
var mA=1
var mB=1
var mC=1
var mD=1
var mE=1
TTrainInfoDlg.initColumn = function () {
    return [
        {title: '运单号', field: 'original_num',width: 100, visible: true, align: 'center', valign: 'middle'},
        {title: '货物名称', field: 'goods_name',width: 100, visible: true, align: 'center', valign: 'middle'},
        {title: '件数', field: 'goods_piece',width: 100, visible: true, align: 'center', valign: 'middle'},
        {title: '送货地址', field: 'receivingAddress',width: 100, visible: true, align: 'center', valign: 'middle'},
        {title: '提付<input type="checkbox" value="" title="月结" lay-filter="aDemo" checked >', field: 'tf', width: 120,visible: true, align: 'center', valign: 'middle'},
        {title: '代收货款<input type="checkbox" value="" title="月结" lay-filter="bDemo" checked>', field: 'ds',width: 150, visible: true, align: 'center', valign: 'middle'},
        {title: '中转费<input type="checkbox" value="" title="月结" lay-filter="cDemo" checked>', field: 'zz',width: 150, visible: true, align: 'center', valign: 'middle'},
        {title: '送货费<input type="checkbox" value="" title="月结" lay-filter="dDemo" checked>', field: 'sh',width: 150, visible: true, align: 'center', valign: 'middle'},
        {title: '搬运费<input type="checkbox" value="" title="月结" lay-filter="eDemo" checked>', field: 'ty',width: 150, visible: true, align: 'center', valign: 'middle'},
        {title: '回单要求', field: 'receipt',width: 100, visible: true, align: 'center', valign: 'middle'}

    ];
};
/**
 * 清除数据
 */
TTrainInfoDlg.clearData = function() {
    this.tTrainInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
TTrainInfoDlg.set = function(key, val) {
    this.tTrainInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
TTrainInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
TTrainInfoDlg.close = function() {
    parent.layer.close(window.parent.TTrain.layerIndex);
}

/**
 * 收集数据
 */
TTrainInfoDlg.collectData = function() {
    this
    .set('id')
    .set('cash')
    .set('weixin')
    .set('alipay')
    .set('bank')
    .set('trainNo')
    .set('mA',mA)
    .set('mB',mB)
    .set('mC',mC)
    .set('mD',mD)
    .set('mE',mE)
        .set('xj',$('#xj').html())
        .set('gz',$('#gz').html())
    ;
}

/**
 * 提交添加
 */
// TTrainInfoDlg.addSubmit = function() {
//
//     this.clearData();
//     this.collectData();
//
//     //提交信息
//     var ajax = new $ax(Feng.ctxPath + "/tTrain/add", function(data){
//         Feng.success("添加成功!");
//         window.parent.TTrain.table.refresh();
//         TTrainInfoDlg.close();
//     },function(data){
//         Feng.error("添加失败!" + data.responseJSON.message + "!");
//     });
//     ajax.set(this.tTrainInfoData);
//     ajax.start();
// }

/**
 * 提交修改
 */
TTrainInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();
    var cash=parseFloat($('#cash').val());
    var weixin=parseFloat($('#weixin').val());
    var alipay=parseFloat($('#alipay').val());
    var bank=parseFloat($('#bank').val());
    var xj=parseFloat($('#xj').text());
    if((cash+weixin+alipay+bank)!=xj){
        Feng.error("收支金额不等!");
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/tTrain/trainHX", function(data){
        Feng.success("核销成功!");
        window.parent.aTable.refresh();
        TTrainInfoDlg.close();
    },function(data){
        Feng.error("核销失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.tTrainInfoData);
    ajax.start();
}
var mDate=null;
TTrainInfoDlg.collectSum = function(m) {
    if(m){
        mDate=m;
    }

    var tf=0;
    var ds=0;
    var zz=0;
    var sh=0;
    var by=0;

    var xj=0;
    var gz=0;
    for(var i=0;i<mDate.data.length;i++){
        if(mA==0){//月结
            xj=xj+parseFloat(mDate.data[i].tf)*-1;
        }else{
            gz=gz+parseFloat(mDate.data[i].tf)*-1;
        }
        if(mB==0){
            xj=xj+parseFloat(mDate.data[i].ds)*-1;
        }else{
            gz=gz+parseFloat(mDate.data[i].ds)*-1;
        }
        if(mC==0){
            xj=xj+parseFloat(mDate.data[i].zz);
        }else{
            gz=gz+parseFloat(mDate.data[i].zz);
        }
        if(mD==0){
            xj=xj+parseFloat(mDate.data[i].sh);
        }else{
            gz=gz+parseFloat(mDate.data[i].sh);
        }
        if(mE==0){
            xj=xj+parseFloat(mDate.data[i].ty);
        }else{
            gz=gz+parseFloat(mDate.data[i].ty);
        }
    }
    $('#xj').text(xj);
    $('#cash').val(xj);
    $('#gz').text(gz);
}

var aTable = null
var mTable = null;
$(function () {
    var defaultColunms = TTrainInfoDlg.initColumn();
    aTable = new LYTable(TTrainInfoDlg.id, "/tTrain/detailList?batch_no="+$('#trainNo').val(), defaultColunms);
    layui.use('table', function () {
        mTable = layui.table;
        TTrainInfoDlg.table = aTable.initCallBack(mTable,TTrainInfoDlg.collectSum);
    var form = layui.form;
        form.on('checkbox(aDemo)', function(obj){
            if(obj.elem.checked){
                mA=1;
            }else{
                mA=0;
            }
            TTrainInfoDlg.collectSum();
        });
        form.on('checkbox(bDemo)', function(obj){
            if(obj.elem.checked){
                mB=1;
            }else{
                mB=0;
            }
            TTrainInfoDlg.collectSum();
        });
        form.on('checkbox(cDemo)', function(obj){
            if(obj.elem.checked){
                mC=1;
            }else{
                mC=0;
            }
            TTrainInfoDlg.collectSum();
        });
        form.on('checkbox(dDemo)', function(obj){
            if(obj.elem.checked){
                mD=1;
            }else{
                mD=0;
            }
            TTrainInfoDlg.collectSum();
        });
        form.on('checkbox(eDemo)', function(obj){
            if(obj.elem.checked){
                mE=1;
            }else{
                mE=0;
            }
            TTrainInfoDlg.collectSum();
        });
    })


});
